package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;


import cn.edu.nuc.onlinestore.Person.ObjectStream;
import cn.edu.nuc.onlinestore.model.Cart;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.network.TCPClient;

import javax.swing.JTable;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class UserCartFrame extends JFrame{

	private JPanel contentPane;
	private JTable table;
    private String name;
    private JFrame frame;
    int temp=0;
	/**
	 * Launch the application.
	 */
     /*	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserCartFrame frame = new UserCartFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
    */
	/**
	 * Create the frame.
	 */
    public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public UserCartFrame(String name) {
		this.frame = this;
		this.name=name;
		setTitle("购物车详情");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 591, 377);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(￥)");
		model.addColumn("数量");
		model.addColumn("总价格(￥)");
		//model.addColumn("操作");
		model.addRow(new String[]{"商品编号","名称","单价(￥)","数量","总价格(￥)"});


		   TCPClient client=new TCPClient();
		   String msg="allCartgoods###"+this.name;
		    HashSet<Goods> setGoods=client.allCartGoods(msg);
			float totalPrice=0;
			int shopNum=0;
			//Map<Integer, String> map=new HashMap<Integer, String>();
			//int i=0;
			/*map.put(0,str1);
			map.put(1,str2);
			map.put(2,str3);
			map.put(3, goods.getBrief());*/
			//int FileNum =setGoods.size();
	       /*
			 System.out.println(setGoods.size());
			 setGoods.forEach((good)->{
				 System.out.println("TCPClient --> ");
				 temp++;
				 System.out.println(temp);
				 System.out.println(good.getGoodsName());
				 System.out.println(good.getGid());
				 System.out.println(good.getPrice());
				 System.out.println(good.getAmount());
			 });*/
	        for(Goods allGoods:setGoods){
	     	
	          model.addRow(new String[] { String.valueOf(allGoods.getGid()), allGoods.getGoodsName(),String.valueOf(allGoods.getPrice()), String.valueOf(allGoods.getAmount()) ,String.valueOf(allGoods.getAmount()*allGoods.getPrice())});
	          //map.put(i,String.valueOf(allGoods.getGid())+"@#@"+String.valueOf(allGoods.getAmount()));
	          totalPrice+=allGoods.getAmount()*allGoods.getPrice();
	          shopNum+=allGoods.getAmount();
	        }
		
	
		table = new JTable( model );
		table.setBounds(10, 38, 543, 184);
		
		contentPane.add( table );
		
		JLabel label = new JLabel("总商品数量: "+shopNum);
		label.setBounds(195, 232, 116, 15);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("总金额: "+totalPrice);
		lblNewLabel.setBounds(342, 232, 211, 15);
		contentPane.add(lblNewLabel);
		
		JButton button = new JButton("结账");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				userCheckOut  user=new userCheckOut(frame,model,name);
				user.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				user.setVisible(true);
				
			}
		});
		button.setBounds(456, 283, 93, 23);
		contentPane.add(button);
		
		
	}
	
}
